package com.yicong.ants.utils

import android.content.Context
import android.os.Build
import com.cchao.simplelib.util.LanguageUtil
import java.util.Locale

class AppUtils2 {
    fun attachBaseContext(context: Context): Context? {
        val resources = context.resources
        val configuration = resources.configuration
        val locale = Locale(
            LanguageUtil.Cur_Language.split("_".toRegex())
                .dropLastWhile { it.isEmpty() }
                .toTypedArray()[0],
            LanguageUtil.Cur_Language.split("_".toRegex())
                .dropLastWhile { it.isEmpty() }
                .toTypedArray()[1])
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //指定了语言使用指定语言，没有则使用首选语言
            configuration.setLocale(locale)
            return context.createConfigurationContext(configuration)
        } else {
            configuration.locale = locale
            val dm = resources.displayMetrics
            resources.updateConfiguration(configuration, dm)
        }
        return context
    }
}